package jtg;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExpectResult {

    public String clsPath;
    public String clsName;
    public String methodName;
    public Class<?>[] paraArray;

    public ExpectResult(String clsPath, String clsName, String methodName, Class<?>[] paraArray) {
        this.clsPath = clsPath;
        this.clsName = clsName;
        this.methodName = methodName;
        this.paraArray = paraArray;
    }

    public Object getResult(Object[] paras) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        URL u = new URL("file:\\" + clsPath + "\\");
        URLClassLoader loader = new URLClassLoader(new URL[]{u});
        Class<?> c = loader.loadClass(clsName);
        Object o = c.newInstance();
        Method m = c.getMethod(methodName, paraArray);
        return m.invoke(o, paras);
    }

    /**
     * @param testSet:[i1=0 i0=21 , i1=20 i0=21 , i1=-3 i0=21 , i1=16 i0=21 , i1=0 i0=20 , i1=6 i0=20 , i1=19 i0=20 ]
     * @Param methodName:multipleIf
     * @Param clsName:LogicStructure
     */
    public static String calJUnit(List<String> testSet, List<String> resultSet,String methodName, String clsName) {

        List<String> testList = new ArrayList<>();
        testSet.forEach(test -> {
            if(test.contains("null")) {
                testList.add("null");
            }else {
                String[] s = test.split(" ");
                StringBuilder res = new StringBuilder();
                for (int i = 0; i < s.length; i++) {
                    res.append(s[i].split("=")[1]);
                    if (i != s.length - 1) {
                        res.append(",");
                    }
                }
                testList.add(res.toString());
            }
        });
        List<String> resultList = new ArrayList<>(resultSet);

        StringBuilder ret = new StringBuilder();
        ret.append("@Test\n").append("void ").append(methodName).append("Test() {\n");
        ret.append("    ").append(clsName).append(" ").append(clsName.toLowerCase()).append(" = new ").append(clsName).append("();\n");
        ret.append("    assertAll(\n");
        for (int i = 0; i < testList.size(); i++) {
            if(!resultList.get(i).equals("该路径无测试用例可达")) {
                ret.append("                () -> assertEquals(").append(clsName.toLowerCase()).append(".")
                        .append(methodName).append("(").append(testList.get(i)).append("), ").append(resultList.get(i)).append(")");
                if (i != testList.size() - 1) {
                    ret.append(",");
                }
                ret.append("\n");
            }
        }
        ret.append("    );\n").append("}");
        return ret.toString();
    }
}
